package lock;

import org.junit.Test;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author jiangwz
 * @create 2019/9/14.
 */
public class CountDownLatchDemo {


    public static void main(String[] args) throws InterruptedException {
        CountDownLatch latch = new CountDownLatch(5);

        for (int i = 0; i < 5; i++) {
            new Thread(() -> {
                System.out.println(Thread.currentThread().getName()+" end");
                latch.countDown();
            }, ""+i).start();
        }

        //等到所有线程结束了,才会往下执行
        latch.await();
        System.out.println("all thread end");

    }

    @Test
    public void test01() throws InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(1);
        CountDownLatch latch = new CountDownLatch(10);
        for (int i = 0; i < 10; i++) {

            executorService.submit(() -> {
                System.out.println(Thread.currentThread().getName()+" end");
                latch.countDown();
            });
        }

        //等到所有线程结束了,才会往下执行
        latch.await();
        System.out.println("all thread end");
    }

}
